package jndi;

import org.dozer.BeanFactory;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * jndi获取资源
 * Created by kevin on 2020/4/1.
 */
@WebServlet("/aServlet")
public class AServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        try {
            //1.创建jndi的上下文
            Context cxt = new InitialContext();
            //2.找到资源,使用的名称与<Resource>元素的name对应
            DataSource dataSource = (DataSource)cxt.lookup("java:comp/env/jdbc/mysql");
            Connection connection = dataSource.getConnection();
            System.out.println("1111111111111111111"+connection.getClass().getName());
            connection.close();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
